package se;


import java.util.Collection;
import java.util.Map;
import java.util.Set;

//HashMap 底层红黑树
public class HashtablePrinciple2 {

   public static void main(String[] args) {
      HashMap2<String, Object> hashMap2 = new HashMap2();
      hashMap2.put("1", "我是1");
      hashMap2.put("2", "我是2");
      hashMap2.put("3", "我是3");
      System.out.println(hashMap2.get("3"));
   }

   public static class HashMap2<K, V> implements Map<K, V> {
      private static final int initSize = 5;//初始化容量
      private final Entry[] table = new Entry[initSize];
      private final boolean fuGai = true;

      public static class Entry<K, V> {
         private K k;
         private V v;
         private Entry<K, V> next;

         public Entry(K k, V v) {
            this.k = k;
            this.v = v;
         }
      }

      @Override
      public int size() {
         return 0;
      }

      @Override
      public boolean isEmpty() {
         return false;
      }

      @Override
      public boolean containsKey(Object key) {
         return false;
      }

      @Override
      public boolean containsValue(Object value) {
         return false;
      }

      @Override
      public V get(Object key) {
         int index = indexFor(key.hashCode(), table.length);
         return (V) table[index].v;
      }

      public V put(K k, V v) {
         //取此k的hashCode,不同对象的hashCode可能相同,后期是通过equeals方法来兜底的，
         int hashCode = k.hashCode();    //需要对hashCode进行扰动 todo
         int index = indexFor(hashCode, table.length); //获取index
         Entry paramEntry = new Entry(k, v);
         Entry inArray = table[index];
         if (inArray == null) {//直接存数据
            inArray = paramEntry;
         } else if (inArray != null) {//使用链表给inArray对象尾插
            Entry goal = null;
            if (inArray.k.equals(inArray.k)) {    //如果此key就是目标
               goal = inArray;
            } else {
               boolean found = false;
               //查询此元素下的子元素,并不断的判断是否为目标key,找不到覆盖,找到根据特性覆盖与否
               while (!inArray.equals(inArray = inArray.next)) {
                  found = true;
               }
               if (found) {
                  goal = inArray;
               }
            }
            if (goal != null) {//找到根据特性覆盖与否
               // todo  覆盖
            } else {
               goal = inArray;
            }
         }
         System.out.println("value:" + table[index]);
         return v;
      }

      // 获取数组索引
      static final int indexFor(int h, int length) {
         return h & (length - 1);//位运算,需要额外处理
      }

      @Override
      public V remove(Object key) {
         return null;
      }

      @Override
      public void putAll(Map<? extends K, ? extends V> m) {

      }

      @Override
      public void clear() {
      }

      @Override
      public Set<K> keySet() {
         return null;
      }

      @Override
      public Collection<V> values() {
         return null;
      }

      @Override
      public Set<Map.Entry<K, V>> entrySet() {
         return null;
      }
   }

}
